//
//  AppDelegate+ui.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/7.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "AppDelegate+ui.h"

#import "NRWTabBarController.h"
#import "NRWNavigationController.h"

#import "NRWHomeVC.h"
#import "NRWBuyCarVC.h"
#import "NRWSellCarVC.h"
#import "NRWMineVC.h"

#import "NRWGuidePageVC.h"

@implementation AppDelegate (ui)

- (void)setupWindow:(NSDictionary *)lunch {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self loadNewFeatureView:lunch];
    
    [self.window makeKeyAndVisible];
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
}
- (void)loadNewFeatureView:(NSDictionary *)launch
{
    NSString *key = (NSString *)kCFBundleVersionKey;
    NSString *lastVersionCode = [NSString stringWithContentsOfFile:[NSString getVersionFilePath] encoding:NSUTF8StringEncoding error:nil];
    NSString *currentVersionCode = [NSBundle mainBundle].infoDictionary[key];
    if ([lastVersionCode isEqualToString:currentVersionCode]) {
        
        [self changWindow];
    }else{
        NRWGuidePageVC *new = [[NRWGuidePageVC alloc] init];
        new.completeBlock = ^{
            // 第一次使用软件
            [currentVersionCode writeToFile:[NSString getVersionFilePath] atomically:YES encoding:NSUTF8StringEncoding error:nil];
            [self changWindow];

        };
        self.window.rootViewController = new;
    }
    
}
-(void)changWindow{
    NRWTabBarController *tabBarVC = [[NRWTabBarController alloc] init];
    
    NRWHomeVC *homeVC = [[NRWHomeVC alloc] init];
    NRWBuyCarVC *buyCarVC = [[NRWBuyCarVC alloc] init];
    NRWSellCarVC *sellCarVC = [[NRWSellCarVC alloc] init];
    NRWMineVC *mineVC = [[NRWMineVC alloc] init];
    
    NRWNavigationController *homeNav = [[NRWNavigationController alloc] initWithRootViewController:homeVC];
    NRWNavigationController *buyCarNav = [[NRWNavigationController alloc] initWithRootViewController:buyCarVC];
    NRWNavigationController *sellCarNav = [[NRWNavigationController alloc] initWithRootViewController:sellCarVC];
    NRWNavigationController *mineNav = [[NRWNavigationController alloc] initWithRootViewController:mineVC];
    
    [tabBarVC addChildViewController:homeNav title:@"首页" image:@"tabbar_home_n" selectImage:@"tabbar_home_s"];
    [tabBarVC addChildViewController:buyCarNav title:@"买车" image:@"tabbar_buycar_n" selectImage:@"tabbar_buycar_s"];
    [tabBarVC addChildViewController:sellCarNav title:@"卖车" image:@"tabbar_salecar_n" selectImage:@"tabbar_salecar_s"];
    [tabBarVC addChildViewController:mineNav title:@"我的" image:@"tabbar_mine_n" selectImage:@"tabbar_mine_s"];
    
    self.window.rootViewController = tabBarVC;
}

-(UIViewController *)getCurrentUIVC
{
    UIViewController  *superVC = [self getCurrentVC];
    
    if ([superVC isKindOfClass:[NRWTabBarController class]]) {
        
        UIViewController  *tabSelectVC = ((NRWTabBarController*)superVC).selectedViewController;
        
        if ([tabSelectVC isKindOfClass:[NRWNavigationController class]]) {
            
            return ((NRWNavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[NRWNavigationController class]]) {
            
            return ((NRWNavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}
+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    
    
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
